using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ScreenManager;
using Microsoft.Xna.Framework.Content;
using LabyrinthProject;

namespace LabirintGUI.Screens
{
    class AboutScreen : ScreenManager.Screen
    {
        SpriteBatch MainSpriteBatch;
        Texture2D Background;
        MouseHandler ourMouse; //An instance of the MouseHandler class
        List<Button> buttonlist; //Our list of buttons
        MouseState mouseState; //MouseState needed for what checking 

        Button dotButtonRightHigh; //An instance of Button        
        Button dotButtonRightMedium; //An instance of Button       
        Button dotButtonRightLow; //An instance of Button        
                   

        public AboutScreen(GraphicsDevice device)
            : base(device, "about")
        {
        }

        public override bool Init(Game game)
        {
            
            Background = game.Content.Load<Texture2D>(@"about");

            Texture2D textureDot = game.Content.Load<Texture2D>(@"dot_button");
            Texture2D textureDotHovered = game.Content.Load<Texture2D>(@"dot_button_hovered");
            Texture2D textureDotClicked = game.Content.Load<Texture2D>(@"dot_button_clicked");

            Rectangle rectangleDotRightHigh = new Rectangle((int)buttonRH.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
                (int)buttonRH.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                 textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightMedium = new Rectangle((int)buttonRM.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRM.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightLow = new Rectangle((int)buttonRL.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRL.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);



            dotButtonRightHigh = new Button(rectangleDotRightHigh, textureDot, textureDotHovered, textureDotClicked, "RH");
            dotButtonRightMedium = new Button(rectangleDotRightMedium, textureDot, textureDotHovered, textureDotClicked, "RM");
            dotButtonRightLow = new Button(rectangleDotRightLow, textureDot, textureDotHovered, textureDotClicked, "RL");

            buttonlist = new List<Button>(); //Actually creating the list
            this.buttonlist.Add(dotButtonRightHigh);
            this.buttonlist.Add(dotButtonRightMedium);
            this.buttonlist.Add(dotButtonRightLow);
            ourMouse = new MouseHandler(new Vector2(0, 0), game.Content.Load<Texture2D>(@"pointer"));

           
            return base.Init(game);
        }



        

        public override void Shutdown()
        {
            base.Shutdown();
        }



        public override void Draw(GameTime gameTime)
        {

            MainSpriteBatch = new SpriteBatch(_device);

            this.MainSpriteBatch.Begin();
            Vector2 position = new Vector2(0, 0);
            Rectangle backgroundRectangle = new Rectangle(0, 0, _device.Viewport.Width, _device.Viewport.Height);
            this.MainSpriteBatch.Draw(Background, backgroundRectangle, Color.White);


            foreach (Button b in buttonlist) //For every button run through the code between the {}
            {
                //For every button in our list, draw it
                b.Draw(MainSpriteBatch);

                if (b.hovering) //If we are hovering
                {
                    //Draw the hover sprite
                    MainSpriteBatch.Draw(b.OnHovering, b.position, Color.White );
                }
                if (b.clicking) //If we are clicking
                {
                    //Draw the clicking sprite.
                    MainSpriteBatch.Draw(b.OnClicking, b.position, Color.White);
                    switch (b.name)
                    {
                        case "RL":
                            SCREEN_MANAGER.goto_screen("main");
                            break;
                        
                    }     
                }
            }
            //Draw the mouse.
            ourMouse.Draw(MainSpriteBatch);
            //Done
            MainSpriteBatch.End();
            


            base.Draw(gameTime);
        }

        private void HandleMouse()
        {
            mouseState = Mouse.GetState(); //Get the current state of the mouse
            foreach (Button b in buttonlist)
            {                     
                if (ourMouse.ButtonClick(b)) //There's only one button, so we just hardcode it.
                    //If we're hovering over the mouse
                    b.hovering = true; //We ARE hovering
                else
                    b.hovering = false; //Not hovering.
                //We don't even need to use ButtonClick() again if we know if we're hovering.
                if (mouseState.LeftButton == ButtonState.Pressed && b.hovering) //If we're clicking with the Left Mouse Button and we're over the button.
                    b.clicking = true; //We ARE clicking
                else
                    b.clicking = false; //Not clicking
            }
        }


        public override void Update(GameTime gameTime)
        {
 
            // Check if m is pressed and go to screen2
            if (Keyboard.GetState().IsKeyDown(Keys.Back))
            {
                SCREEN_MANAGER.goto_screen("main");
            }
            HandleMouse(); //Check clicking
            ourMouse.Update();
            base.Update(gameTime);
        }
    }
}
